package com.t216.dm216.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.t216.dm216.mapper.DmItemTypeMapper;
import com.t216.dm216.pojo.DmItemType;
import com.t216.dm216.pojo.dto.DmItemTypeDTO;
import com.t216.dm216.pojo.dto.DmItemTypeFloorDTO;
import com.t216.dm216.service.DmItemService;
import com.t216.dm216.service.DmItemTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DmItemTypeServiceImpl extends ServiceImpl<DmItemTypeMapper, DmItemType> implements DmItemTypeService {
    @Resource
    private DmItemTypeMapper dmItemTypeMapper;
    @Resource
    private DmItemService dmItemService;

    @Override
    public List<DmItemTypeDTO> queryAllType() {        /*lambdaWrapper:中的查询信息 是主查询的数据*/
        MPJLambdaWrapper<DmItemType> lambdaWrapper = new MPJLambdaWrapper();
        lambdaWrapper.eq(DmItemType::getLevel,1);
        lambdaWrapper.select(DmItemType::getId, DmItemType::getItemType, DmItemType::getLevel, DmItemType::getParent, DmItemType::getAliasName);
        /*映射了子分类数组*/
        lambdaWrapper.leftJoin(DmItemType.class,DmItemType::getParent,DmItemType::getId);
        /*selectCollection关联的是子查询的信息*/
        lambdaWrapper.selectCollection(DmItemType.class,DmItemTypeDTO::getChildren,ext->
                ext.id(DmItemType::getId)
                        .result(DmItemType::getItemType)
                        .result(DmItemType::getLevel)
                        .result(DmItemType::getParent)
                        .result(DmItemType::getAliasName)
        );

        List<DmItemTypeDTO> list = dmItemTypeMapper.selectJoinList(DmItemTypeDTO.class,lambdaWrapper);

        list.forEach(item->{
            item.setHotItems(dmItemService.getBannerItemByItemType1Id(item.getId()));
        });

        return list;
    }

    @Override
    public List<DmItemType> queryLevel1Type() {
        LambdaQueryWrapper<DmItemType> lambdaWrapper = new LambdaQueryWrapper<>();
        lambdaWrapper.select(DmItemType::getId, DmItemType::getItemType, DmItemType::getLevel, DmItemType::getAliasName, DmItemType::getKey);
        lambdaWrapper.eq(DmItemType::getLevel,1);
        return dmItemTypeMapper.selectList(lambdaWrapper);
    }

    @Override
    public List<DmItemTypeFloorDTO> queryFloorItems() {
        MPJLambdaWrapper<DmItemType> lambdaWrapper = new MPJLambdaWrapper();
        lambdaWrapper.eq(DmItemType::getLevel,1);
        lambdaWrapper.selectAs(DmItemType::getItemType,"itemTypeName");
        lambdaWrapper.selectAs(DmItemType::getId,"itemTypeId");
        lambdaWrapper.selectAs(DmItemType::getKey,"floor");

        List<DmItemTypeFloorDTO> list = dmItemTypeMapper.selectJoinList(DmItemTypeFloorDTO.class,lambdaWrapper);
        list.forEach(item->{
            item.setItems(dmItemService.getFloorItemsByItemType1Id(item.getItemTypeId()));
        });

        return list;
    }

    @Override
    public List<DmItemType> getItemType(Integer parent) {
        LambdaQueryWrapper<DmItemType> lambdaWrapper = new LambdaQueryWrapper<>();
        lambdaWrapper.select(DmItemType::getAliasName, DmItemType::getId, DmItemType::getLevel, DmItemType::getParent, DmItemType::getItemType);
        lambdaWrapper.eq(DmItemType::getParent,parent);

        return dmItemTypeMapper.selectList(lambdaWrapper);
    }
}
